/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2021-11-03 10:27:19
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-04-22 10:06:09
 * @FilePath: \code\cwp-front\pc-collaboration\src\handles\common\add-node\index.js
 * @Description:加签
 */
import { getInsertNodeParams } from './helper';
import {useBaseInfoWithOut} from "@/store/modules/base-info";
import emitter from "@/utils/emitter";
import globalData from '@/plugins/data';
import {fireEvent, getCollEventParams} from "@/handles/common/sdk";
import {useBaseInfoStore} from "@/store/modules/base-info";

/**
 * @description: 加签操作
 * @param {String|Object} people 如果传入代表加签给指定人
 * @param {Function} resolve
 * @return {*}
 */
export async function addNode(people, resolve=()=>{}) {
    let validateProcess = useBaseInfoWithOut().validateProcess();
    if (!validateProcess) {
        return resolve();
    }

    const businessData = useBaseInfoStore().baseInfo?.businessData || {};
    const eventParams = getCollEventParams(businessData, globalData);
    let eventResult = await fireEvent("AddNode", eventParams);
    if (!eventResult) {
        return resolve();
    }

    await emitter.publish("sendFreeCol");
    var processXml = document.querySelector('#process_xml');
    if (!processXml) {
        processXml = document.createElement('input');
        processXml.type = 'hidden';
        processXml.id = 'process_xml';
        document.body.appendChild(processXml);
    }

    var readyObjectJSON = document.querySelector('#readyObjectJSON');
    if (!readyObjectJSON) {
        readyObjectJSON = document.createElement('input');
        readyObjectJSON.type = 'hidden';
        readyObjectJSON.id = 'readyObjectJSON';
        document.body.appendChild(readyObjectJSON);
    }

    insertNode(await getInsertNodeParams.call(this, people, 'addNode', resolve));
}

export default function () {
    return new Promise((resolve) => {
        addNode.call(this);
        resolve();
    });
}
